<?php
namespace model;

class Login{
		
	private static $logOutButton = 'logOutButton';
	private static $logInButton = 'logInButton';
	private static $cookieUsername = 'cookieUsername';
	private static $cookiePassword = 'cookiePassword';
	private static $username = 'username';
	private static $password = 'password';	
	private static $salt = "salt";
	private static $loggedIn = 'loggedIn';
	private static $Admin = 'Admin';
	private static $Password = 'Password';
	private static $checkbox = 'checkbox';
	//@var array[]
	private $array;
	
	/*
	 * @var array[]
	 * return string (message)
	 */	
	public function checkControl($arg){
			
		$this->array = $arg;
		
		if(isset($this->array[self::$logOutButton])){
			$message = $this->logOut();
		}else if(isset($_SESSION[self::$loggedIn])){
			$message = $this->checkSession();
		}else if(isset($this->array[self::$cookieUsername]) && isset($this->array[self::$cookiePassword])){
			$message = $this->checkCookies();
		}else if(isset($this->array[self::$logInButton])){
			$message = $this->tryLogIn();
		}else{
				$message = "";
		}
		return $message;
	}
	
	/*
	 * checks cookies
	 * return string (message)
	 */
	public function checkCookies(){
		
		if ($this->checkTimeOnCookie() == FALSE){
			$message = "Cookie to old!";
			return $message;
		}
		if($this->array[self::$cookieUsername] == self::$Admin && $this->array[self::$cookiePassword] == md5(self::$Password . self::$salt)){
			$message = 'Inloggning lyckades med cookies nomnomnom';
			$this->createSession();
			
		}else{
			$message = "Fel information i cookies ";
		}
		return $message;
	}
	
	/*
	 * try to log in
	 * return string (message)
	 */
	public function tryLogIn(){
		try{
				if ($this->array[self::$username] == self::$Admin && $this->array[self::$password] == self::$Password ){
						$message = 'Inloggning lyckades';
					if(isset($this->array[self::$checkbox])){
						$message .= ' och dina uppgifter är sparade';
					}
					$this->createSession();
					return $message;
				}else{
					if(empty($this->array[self::$username]) && empty($this->array[self::$password])){
						$message = 'Användarnamn saknas';
					}
					else if(empty($this->array[self::$username])){
						$message = 'Användarnamn saknas!';
					}
					else if(empty($this->array[self::$password])){
						$message = 'Lösenord saknas';
					}
					else{
						$message = 'Felaktigt användarnamn och/eller lösenord';
					}
					return $message;
				}
		}catch(exception $e){echo"Something wrong in model";}
	}
	
	public function checkIfLoggedIn(){
		try{
				if(isset($_SESSION['loggedIn'])){
					$sessionLoggedIn = TRUE;
				}else{
					$sessionLoggedIn = FALSE;
				}
				return $sessionLoggedIn;
			}
		catch(Exception $e){
			echo "Something wrong in model";
		}
	}
	
	//logs out || destroys sesseions and cookies
	//return string (message)
	public function logOut(){
			
			unset($_SESSION[self::$loggedIn]);
			session_destroy();
			$message = 'Du har loggats ut';
		    return $message;
		}

	/*
	 * checks time on cookie
	 */
	private function checkTimeOnCookie(){
		$cookieEndTime = file_get_contents("endtime.txt");
		try{
			if(time() < $cookieEndTime){
				return true;
			}else if (time() > $cookieEndTime){
				return false;
			}
		}catch(exception $e){
			echo "Something wrong in model";
		}
		
	}

	/*
	 * create session 
	 */
	private function createSession(){
		$_SESSION[self::$loggedIn] = true;
		
		$_SESSION["session_security"] = array();
		$_SESSION["session_security"]["webbläsare"] = $this->getUserAgent();
	}
	
	/*
	 * checks session
	 */
	public function checkSession(){
		if($_SESSION["session_security"]["webbläsare"] == $this->getUserAgent()){
			return $message = "";
		}else {
			unset($_SESSION[self::$loggedIn]);
			session_destroy();
			return $message = "Sessions Tjuv!";
		}		
	}
	
	// Magic happens in this function to find out the users browser
	//http://stackoverflow.com/questions/9693574/user-agent-extract-os-and-browser-from-string-php
	public static function getUserAgent(){
    static $agent = null;

    if ( empty($agent) ) {
        $agent = $_SERVER['HTTP_USER_AGENT'];

        if ( stripos($agent, 'Firefox') !== false ) {
            $agent = 'firefox';
        } elseif ( stripos($agent, 'MSIE') !== false ) {
            $agent = 'ie';
        } elseif ( stripos($agent, 'iPad') !== false ) {
            $agent = 'ipad';
        } elseif ( stripos($agent, 'Android') !== false ) {
            $agent = 'android';
        } elseif ( stripos($agent, 'Chrome') !== false ) {
            $agent = 'chrome';
        } elseif ( stripos($agent, 'Safari') !== false ) {
            $agent = 'safari';
        } elseif ( stripos($agent, 'AIR') !== false ) {
            $agent = 'air';
        } elseif ( stripos($agent, 'Fluid') !== false ) {
            $agent = 'fluid';
        }

    }
    return $agent;
	}
}





